from selenium.webdriver import ActionChains
from selenium import webdriver
import time
import pymongo
from lxml import etree
from my_fake_useragent import UserAgent
client  = pymongo.MongoClient('localhost',27017)
mydb = client['pymongo']
taobao = mydb['tabao']
options = webdriver.ChromeOptions()
options.add_argument('lang=zh_CN.UTF-8')
options.add_argument('user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"')
browser = webdriver.Chrome(options=options)
# browser.maximize_window()
action = ActionChains(browser)
def get_info(url,page):
    page = page + 1
    browser.get(url)
    browser.implicitly_wait(5)
    selector = etree.HTML(browser.page_source)
    infos = selector.xpath('//div[@class="item J_MouserOnverReq  "]')
    for info in infos:
        data = info.xpath('div/div/a')[0]
        goods = data.xpath('string(.)').strip()
        sell = info.xpath('div/div/div[@class="deal-cnt"]/text()')[0]
        shop = info.xpath('div[2]/div[3]/div[1]/a/span[2]/text()')[0]
        address = info.xpath('div[2]/div[3]/div[2]/text()')[0]
        commodity = {
            'good': goods,
            'sell': sell,
            'shop': shop,
            'address': address
        }
        taobao.insert_one(commodity)

        if page <= 50:
            NextPage(url,page)
        else:
            pass
def NextPage(url,page):
    browser.get(url)
    browser.implicitly_wait(5)
    browser.find_element_by_xpath('//a[@trace="srp_bottom_pagedown"]').click()
    time.sleep(2)
    print(browser.current_url)
    browser.get(browser.current_url)
    browser.implicitly_wait(5)
    get_info(browser.current_url,page)

if __name__ == '__main__':
    username = input('用户名')
    password = input('密码')
    page = 1
    url = 'https://taobao.com/'
    browser.get(url)
    browser.implicitly_wait(5)
    browser.find_element_by_id('q').clear()
    browser.find_element_by_id('q').send_keys('短袖男')
    browser.find_element_by_class_name('btn-search').click()
    try:
        browser.find_element_by_class_name('login-box-warp')
        a = True
    except:
        a = False

    if a  == True:
        browser.find_element_by_xpath('//*[@id="J_QRCodeLogin"]/div[5]/a[1]').click()
        browser.find_element_by_id('TPL_username_1').clear()
        browser.find_element_by_id('TPL_username_1').send_keys(username)
        browser.find_element_by_id('TPL_password_1').clear()
        browser.find_element_by_id('TPL_password_1').send_keys(password)
        browser.find_element_by_id('J_SubmitStatic').click()
    browser.implicitly_wait(5)
    get_info(browser.current_url,page)
